package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @program: duolaimall
 * @description:
 * @author: Lexis
 * @create: 2022-05-19 22:52
 **/
@RestController
@RequestMapping("shopping")
public class CartController {

    @Autowired
    ICartService cartService;

    @PostMapping("carts")
    public ResponseData addCart(@RequestBody Map map) {
        String userId = (String) map.get("userId");
        Integer itemId = (Integer) map.get("productId");
        Integer num = (Integer) map.get("productNum");
        AddCartRequest request = new AddCartRequest();
        request.setNum(num);
        request.setItemId(Long.valueOf(itemId));
        request.setUserId(Long.valueOf(userId));

        AddCartResponse addCartResponse = cartService.addToCart(request);
        if (addCartResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(null);
        }
        return new ResponseUtil<>().setErrorMsg(addCartResponse.getMsg());
    }

    @GetMapping("carts")
    public ResponseData showCarts(HttpServletRequest request) {
        String user_info = request.getHeader("user_info");
        JSONObject object = JSON.parseObject(user_info);
        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(Long.valueOf((Integer) object.get("id")));
        CartListByIdResponse response = cartService.getCartListById(cartListByIdRequest);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getCartProductDtos());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @PutMapping("carts")
    public ResponseData putCarts(@RequestBody Map map) {
        String userId = (String) map.get("userId");
        Integer itemId = (Integer) map.get("productId");
        Integer num = (Integer) map.get("productNum");
        Object checked = map.get("checked");
        UpdateCartNumRequest request = new UpdateCartNumRequest();
        if (checked instanceof Boolean) {
            if ((Boolean) checked) {
                request.setChecked("true");
            } else {
                request.setChecked("false");
            }
        }
        if (checked instanceof String) {
            request.setChecked((String) checked);
        }
        request.setNum(num);
        request.setItemId(Long.valueOf(itemId));
        request.setUserId(Long.valueOf(userId));
        UpdateCartNumResponse response = cartService.updateCartNum(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData("成功");
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("carts/{uid}/{pid}")
    public ResponseData deleteCarts(@PathVariable("uid") Long uid, @PathVariable("pid") Long pid) {
        DeleteCartItemRequest request = new DeleteCartItemRequest();
        request.setUserId(uid);
        request.setItemId(pid);
        DeleteCartItemResponse response = cartService.deleteCartItem(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData("成功");
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("items/{uid}")
    public ResponseData deleteCheckedCarts(@PathVariable("uid") Long uid) {
        DeleteCheckedItemRequest request = new DeleteCheckedItemRequest();
        request.setUserId(uid);
        DeleteCheckedItemResposne response = cartService.deleteCheckedItem(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData("成功");
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @PutMapping("items")
    public ResponseData checkAllCarts(@RequestBody CheckAllItemRequest request) {
        CheckAllItemResponse response = cartService.checkAllCartItem(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData("成功");
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }
}
